﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CMSMVC4.Areas.Main.Models;
using CMSMVC4.Filters;

namespace CMSMVC4.Areas.Main.Controllers
{
    public class RssController : Controller
    {
        //
        // GET: /Admin/Sitemap/

        public ActionResult Index()
        {
            ViewBag.Title = "Rss";
            return View();
        }

        public ActionResult TinTuc()
        {
            List<ItemRss> posts = new List<ItemRss>();
            CMSMVC4Entities db = new CMSMVC4Entities();
            //-------------------Tin Tuc----------------
            var Page = db.Pages.Where(x => x.Category.Type == MenuType.TinTuc && x.Status == PageStatus.Thuong).ToList();
            if (Page.Count > 0)
            {
                foreach (var tb in Page)
                {
                    ItemRss pt = new ItemRss();
                    var name = tb.PageLangs.FirstOrDefault(x => x.PageId == tb.Id && x.LanguageCode.Equals("vi-vn"));
                    if (name != null)
                    {
                        pt.title = name.Name;
                        pt.description = name.Summary;
                    }
                    pt.link = "/TinTuc/BaiViet/" + tb.Id;
                    if (tb.Date != null) pt.pubDate = (DateTime) tb.Date;
                    posts.Add(pt);
                }

            }
            //-----------------------------------------
            RssActionResults sm = new RssActionResults();
            sm.RssActionResult(posts);
            return sm;
        }

        public ActionResult ThietBi()
        {
            List<ItemRss> posts = new List<ItemRss>();
            CMSMVC4Entities db = new CMSMVC4Entities();
            //-------------------thiet bi----------------
            var categori = db.Categories.Where(x => x.Type == MenuType.ThiteBi && x.Status == CategoriStatus.Thuong).ToList();
            if (categori.Count > 0)
            {
                foreach (var tb in categori)
                {
                    ItemRss pt = new ItemRss();
                    var name = tb.CateLangs.FirstOrDefault(x => x.CategoriesId == tb.Id && x.LanguageCode.Equals("vi-vn"));
                    if (name != null)
                    {
                        pt.title = name.Name;
                        pt.description = name.Name;
                    }
                    pt.link = "/ThietBi/Index/" + tb.Id;
                    pt.pubDate = DateTime.Now;
                    posts.Add(pt);
                }

            }
            //-----------------------------------------
            RssActionResults sm = new RssActionResults();
            sm.RssActionResult(posts);
            return sm;
        }

        public ActionResult DichVu()
        {
            List<ItemRss> posts = new List<ItemRss>();
            CMSMVC4Entities db = new CMSMVC4Entities();
            //-------------------thiet bi----------------
            var categori = db.Categories.Where(x => x.Type == MenuType.DichVu && x.Status == CategoriStatus.Thuong).ToList();
            if (categori.Count > 0)
            {
                foreach (var tb in categori)
                {
                    ItemRss pt = new ItemRss();
                    var name = tb.CateLangs.FirstOrDefault(x => x.CategoriesId == tb.Id && x.LanguageCode.Equals("vi-vn"));
                    if (name != null)
                    {
                        pt.title = name.Name;
                        pt.description = name.Name;
                    }
                    pt.link = "/ThietBi/Index/" + tb.Id;
                    pt.pubDate = DateTime.Now;
                    posts.Add(pt);
                }

            }
            //-----------------------------------------
            RssActionResults sm = new RssActionResults();
            sm.RssActionResult(posts);
            return sm;
        }
    }
}
